import fs from 'fs'
import path from 'path'
import yaml from 'yaml'
import lodash from 'lodash'

export function supportGuoba () {
  return {
    pluginInfo: {
      name: 'Slave-Market',
      title: '奴隶市场',
      description: '奴隶市场配置中心',
      author: '@Tloml-Starry',
      authorLink: 'https://gitee.com/Tloml-Starry',
      link: 'https://gitee.com/Tloml-Starry/Slave-Market',
      isV3: true,
      isV2: false,
      showInMenu: true,
      icon: 'mdi:store',
      iconColor: '#8b4513',
      iconPath: `${path.join(path.resolve(), 'plugins', 'Slave-Market')}/resources/psc.png`
    },
    configInfo: {
      schemas: [
        {
          component: 'Divider',
          label: '回购设置',
          componentProps: {
            orientation: 'left',
            plain: true
          }
        },
        {
          field: 'buyBack.cooldown',
          label: '回购冷却时间',
          bottomHelpMessage: '设置回购操作的冷却时间（秒）',
          component: 'InputNumber',
          required: true,
          componentProps: {
            min: 1,
            max: 999999,
            placeholder: '请输入冷却时间',
            addonAfter: '秒'
          }
        },
        {
          field: 'buyBack.maxTimes',
          label: '最大回购次数',
          bottomHelpMessage: '设置最大回购次数限制',
          component: 'InputNumber',
          required: true,
          componentProps: {
            min: 1,
            max: 99,
            placeholder: '请输入最大次数',
            addonAfter: '次'
          }
        },
        {
          field: 'buyBack.taxRate',
          label: '回购税率',
          bottomHelpMessage: '设置回购时的税率（0-1之间）',
          component: 'InputNumber',
          required: true,
          componentProps: {
            min: 0,
            max: 1,
            step: 0.01,
            placeholder: '请输入税率'
          }
        },
        {
          component: 'Divider',
          label: '抢劫设置',
          componentProps: {
            orientation: 'left',
            plain: true
          }
        },
        {
          field: 'rob.cooldown',
          label: '抢劫冷却时间',
          bottomHelpMessage: '设置抢劫操作的冷却时间（秒）',
          component: 'InputNumber',
          required: true,
          componentProps: {
            min: 1,
            max: 999999,
            placeholder: '请输入冷却时间',
            addonAfter: '秒'
          }
        },
        {
          field: 'rob.successRate',
          label: '抢劫成功率',
          bottomHelpMessage: '设置抢劫的成功概率（0-1之间）',
          component: 'InputNumber',
          required: true,
          componentProps: {
            min: 0,
            max: 1,
            step: 0.01,
            placeholder: '请输入成功率'
          }
        },
        {
          field: 'rob.penalty',
          label: '抢劫失败惩罚',
          bottomHelpMessage: '设置抢劫失败的惩罚系数（0-1之间）',
          component: 'InputNumber',
          required: true,
          componentProps: {
            min: 0,
            max: 1,
            step: 0.01,
            placeholder: '请输入惩罚系数'
          }
        },
        {
          component: 'Divider',
          label: '工作设置',
          componentProps: {
            orientation: 'left',
            plain: true
          }
        },
        {
          field: 'work.cooldown',
          label: '工作冷却时间',
          bottomHelpMessage: '设置普通工作的冷却时间（秒）',
          component: 'InputNumber',
          required: true,
          componentProps: {
            min: 1,
            max: 999999,
            placeholder: '请输入冷却时间',
            addonAfter: '秒'
          }
        },
        {
          field: 'work.slaveownerCooldown',
          label: '奴隶主冷却时间',
          bottomHelpMessage: '设置奴隶主工作的冷却时间（秒）',
          component: 'InputNumber',
          required: true,
          componentProps: {
            min: 1,
            max: 999999,
            placeholder: '请输入冷却时间',
            addonAfter: '秒'
          }
        },
        {
          component: 'Divider',
          label: '银行设置',
          componentProps: {
            orientation: 'left',
            plain: true
          }
        },
        {
          field: 'bank.initialLimit',
          label: '初始存储上限',
          bottomHelpMessage: '设置初始银行存储上限',
          component: 'InputNumber',
          required: true,
          componentProps: {
            min: 100,
            placeholder: '请输入初始存储上限',
            addonAfter: '金币'
          }
        },
        {
          field: 'bank.initialLevel',
          label: '初始信用等级',
          bottomHelpMessage: '设置初始信用等级',
          component: 'InputNumber',
          required: true,
          componentProps: {
            min: 1,
            placeholder: '请输入初始等级'
          }
        },
        {
          field: 'bank.upgradePriceMulti',
          label: '升级价格倍数',
          bottomHelpMessage: '设置每次升级价格的增长倍数',
          component: 'InputNumber',
          required: true,
          componentProps: {
            min: 1,
            step: 0.1,
            placeholder: '请输入倍数'
          }
        },
        {
          field: 'bank.limitIncreaseMulti',
          label: '存储上限增长倍数',
          bottomHelpMessage: '设置每次升级存储上限的增长倍数',
          component: 'InputNumber',
          required: true,
          componentProps: {
            min: 1,
            step: 0.1,
            placeholder: '请输入倍数'
          }
        },
        {
          field: 'bank.initialUpgradePrice',
          label: '初始升级价格',
          bottomHelpMessage: '设置第一次升级所需金币',
          component: 'InputNumber',
          required: true,
          componentProps: {
            min: 1,
            placeholder: '请输入价格',
            addonAfter: '金币'
          }
        },
        {
          field: 'bank.interestRate',
          label: '每小时利息率',
          bottomHelpMessage: '设置每小时的存款利息率（如：0.01表示1%）',
          component: 'InputNumber',
          required: true,
          componentProps: {
            min: 0.001,
            max: 0.1,
            step: 0.001,
            placeholder: '请输入利息率'
          }
        },
        {
          field: 'bank.maxInterestTime',
          label: '最大计息时间',
          bottomHelpMessage: '设置最大计息时间（小时）',
          component: 'InputNumber',
          required: true,
          componentProps: {
            min: 1,
            max: 72,
            placeholder: '请输入小时数',
            addonAfter: '小时'
          }
        },
        {
          component: 'Divider',
          label: '训练系统设置',
          componentProps: {
            orientation: 'left',
            plain: true
          }
        },
        {
          field: 'training.cooldown',
          label: '训练冷却时间',
          bottomHelpMessage: '设置训练操作的冷却时间（秒）',
          component: 'InputNumber',
          required: true,
          componentProps: {
            min: 1,
            max: 999999,
            placeholder: '请输入冷却时间',
            addonAfter: '秒'
          }
        },
        {
          field: 'training.successRate',
          label: '训练成功率',
          bottomHelpMessage: '设置训练的成功概率（0-1之间）',
          component: 'InputNumber',
          required: true,
          componentProps: {
            min: 0,
            max: 1,
            step: 0.01,
            placeholder: '请输入成功率'
          }
        },
        {
          field: 'training.costRate',
          label: '训练费用比例',
          bottomHelpMessage: '设置训练费用占奴隶价值的比例',
          component: 'InputNumber',
          required: true,
          componentProps: {
            min: 0,
            max: 1,
            step: 0.01,
            placeholder: '请输入费用比例'
          }
        },
        {
          field: 'training.valueIncreaseRate',
          label: '价值提升比例',
          bottomHelpMessage: '设置训练成功时价值提升的比例',
          component: 'InputNumber',
          required: true,
          componentProps: {
            min: 0,
            max: 1,
            step: 0.01,
            placeholder: '请输入提升比例'
          }
        },
        {
          component: 'Divider',
          label: '排位赛设置',
          componentProps: {
            orientation: 'left',
            plain: true
          }
        },
        {
          field: 'ranking.cooldown',
          label: '排位赛冷却时间',
          bottomHelpMessage: '设置排位赛的冷却时间（秒）',
          component: 'InputNumber',
          required: true,
          componentProps: {
            min: 1,
            max: 999999,
            placeholder: '请输入冷却时间',
            addonAfter: '秒'
          }
        },
        {
          field: 'ranking.baseReward',
          label: '基础奖励',
          bottomHelpMessage: '设置排位赛的基础奖励金币',
          component: 'InputNumber',
          required: true,
          componentProps: {
            min: 1,
            placeholder: '请输入基础奖励',
            addonAfter: '金币'
          }
        },
        {
          field: 'ranking.winBonus',
          label: '胜利奖励比例',
          bottomHelpMessage: '设置胜利额外奖励比例（0-1之间）',
          component: 'InputNumber',
          required: true,
          componentProps: {
            min: 0,
            max: 1,
            step: 0.01,
            placeholder: '请输入奖励比例'
          }
        },
        {
          field: 'ranking.tierBonus.青铜',
          label: '青铜段位倍率',
          bottomHelpMessage: '设置青铜段位的奖励倍率',
          component: 'InputNumber',
          required: true,
          componentProps: {
            min: 1,
            step: 0.1,
            placeholder: '请输入倍率'
          }
        },
        {
          field: 'ranking.tierBonus.白银',
          label: '白银段位倍率',
          bottomHelpMessage: '设置白银段位的奖励倍率',
          component: 'InputNumber',
          required: true,
          componentProps: {
            min: 1,
            step: 0.1,
            placeholder: '请输入倍率'
          }
        },
        {
          field: 'ranking.tierBonus.黄金',
          label: '黄金段位倍率',
          bottomHelpMessage: '设置黄金段位的奖励倍率',
          component: 'InputNumber',
          required: true,
          componentProps: {
            min: 1,
            step: 0.1,
            placeholder: '请输入倍率'
          }
        },
        {
          field: 'ranking.tierBonus.铂金',
          label: '铂金段位倍率',
          bottomHelpMessage: '设置铂金段位的奖励倍率',
          component: 'InputNumber',
          required: true,
          componentProps: {
            min: 1,
            step: 0.1,
            placeholder: '请输入倍率'
          }
        },
        {
          field: 'ranking.tierBonus.钻石',
          label: '钻石段位倍率',
          bottomHelpMessage: '设置钻石段位的奖励倍率',
          component: 'InputNumber',
          required: true,
          componentProps: {
            min: 1,
            step: 0.1,
            placeholder: '请输入倍率'
          }
        },
        {
          component: 'Divider',
          label: '转账设置',
          componentProps: {
            orientation: 'left',
            plain: true
          }
        },
        {
          field: 'transfer.feeRate',
          label: '手续费率',
          bottomHelpMessage: '设置转账手续费率(0-1之间)',
          component: 'InputNumber',
          required: true,
          componentProps: {
            min: 0,
            max: 1,
            step: 0.01,
            placeholder: '请输入手续费率'
          }
        },
        {
          field: 'transfer.minAmount',
          label: '最低转账金额',
          bottomHelpMessage: '设置单次最低转账金额',
          component: 'InputNumber',
          required: true,
          componentProps: {
            min: 1,
            step: 1,
            placeholder: '请输入最低金额'
          }
        },
        {
          component: 'Divider',
          label: '购买设置',
          componentProps: {
            orientation: 'left',
            plain: true
          }
        },
        {
          field: 'purchase.cooldown',
          label: '购买冷却时间',
          bottomHelpMessage: '设置购买奴隶的冷却时间（秒）',
          component: 'InputNumber',
          required: true,
          componentProps: {
            min: 1,
            max: 999999,
            placeholder: '请输入冷却时间',
            addonAfter: '秒'
          }
        },
        {
          component: 'Divider',
          label: '竞技场设置',
          componentProps: {
            orientation: 'left',
            plain: true
          }
        },
        {
          field: 'arena.cooldown',
          label: '竞技冷却时间',
          bottomHelpMessage: '设置竞技场挑战的冷却时间（秒）',
          component: 'InputNumber',
          required: true,
          componentProps: {
            min: 1,
            max: 999999,
            placeholder: '请输入冷却时间',
            addonAfter: '秒'
          }
        },
        {
          field: 'arena.entryFee',
          label: '参赛费用',
          bottomHelpMessage: '设置参加竞技场需要支付的费用',
          component: 'InputNumber',
          required: true,
          componentProps: {
            min: 1,
            placeholder: '请输入参赛费用',
            addonAfter: '金币'
          }
        },
        {
          field: 'arena.rewardRate',
          label: '获胜奖励比例',
          bottomHelpMessage: '设置获胜奖励比例（0-1之间）',
          component: 'InputNumber',
          required: true,
          componentProps: {
            min: 0,
            max: 1,
            step: 0.01,
            placeholder: '请输入奖励比例'
          }
        },
        {
          field: 'arena.valueBonus',
          label: '价值提升比例',
          bottomHelpMessage: '设置获胜者价值提升比例（0-1之间）',
          component: 'InputNumber',
          required: true,
          componentProps: {
            min: 0,
            max: 1,
            step: 0.01,
            placeholder: '请输入提升比例'
          }
        }
      ],
      getConfigData () {
        const _path = process.cwd().replace(/\\/g, '/')
        let cfgyaml = `${_path}/config/SlaveMarket_config.yaml`
        const configData = fs.readFileSync(cfgyaml, 'utf8')
        let config = yaml.parse(configData)
        return config
      },

      async setConfigData (data, { Result }) {
        const configFilePath = path.join('config/SlaveMarket_config.yaml')
        let config = {}
        if (fs.existsSync(configFilePath)) {
          const configContent = fs.readFileSync(configFilePath, 'utf8')
          config = yaml.parse(configContent) || {}
        }
        for (const [keyPath, value] of Object.entries(data)) {
          lodash.set(config, keyPath, value)
        }
        const updatedConfigYAML = yaml.stringify(config)
        fs.writeFileSync(configFilePath, updatedConfigYAML, 'utf8')
        logger.mark('[SlaveMarket]配置文件更新')
        return Result.ok({}, '保存成功~')
      }
    }
  }
}
